﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Configuration;
using System.Net;


public partial class login : System.Web.UI.Page
{
    private string connectionString = WebConfigurationManager.ConnectionStrings["bugger"].ConnectionString;
    private int passHashCode;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnlogin_Click(object sender, EventArgs e)
    {
        passHashCode = txtpassword.Text.GetHashCode();
        SqlConnection userConnection = new SqlConnection(connectionString);
        
        userConnection.Open();

        SqlCommand login = new SqlCommand("SELECT * from users WHERE username =@username and password=@password", userConnection);

        login.Parameters.AddWithValue("@username", txtusername.Text);
        login.Parameters.AddWithValue("@password", passHashCode);
        SqlDataAdapter da = new SqlDataAdapter(login);
        DataTable dt = new DataTable();
        da.Fill(dt);

        if (dt.Rows.Count > 0)
        {

            HttpCookie cookie = new HttpCookie("Users");
            cookie["User"] = txtusername.Text;
            Response.Cookies.Add(cookie);
            cookie.Expires = DateTime.Now.AddDays(30);

            Session["User"] = txtusername.Text;
            Response.Redirect("Default.aspx");
        }
        else
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
        }

    }
    protected void chkrememberme_CheckedChanged(object sender, EventArgs e)
    {
        if (chkrememberme.Checked)
        {
            HttpCookie cookie = new HttpCookie("rememberme");

            cookie["UserName"] = txtusername.Text;
            cookie["Password"] = txtpassword.Text.GetHashCode().ToString();

            Response.Cookies.Add(cookie);

            cookie.Expires = DateTime.Now.AddDays(30);
        }
    }
}